import { _decorator, Component, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('RollBase')
export abstract class RollBase extends Component
{

    public MaxHP: number // 最大生命值
    public CurHP: number // 当前生命值
    public CurPos: Vec3 
    public MoveSpeed:number // 移动速度

    OnHitBack(force: number, source: Node){}
    OnHurt(dmg: number, source: Node){}
    OnKill(source: Node){}

    /*
    生命周期要理好
    OnLoad
    Start
    Update
    Hurt
    HitBack
    Kill
    */

    onLoad()
    {
        this.CurPos = new Vec3()
    }

    HitBack(force: number, source: Node)
    {
        this.OnHitBack(force,source)
        let way = new Vec3()
        this.CurPos = this.node.getPosition()
        Vec3.subtract(way, this.CurPos, source.getPosition())
        Vec3.multiplyScalar(way, way.normalize(), force)
        Vec3.add(this.CurPos, this.CurPos, way)
        this.node.setPosition(this.CurPos)
    }

    Hurt(dmg: number, source: Node = null)
    {
        this.CurHP -= dmg
        this.OnHurt(dmg,source)
        if (this.CurHP <= 0)
        {
            this.Kill(source)
        }
    }

    Kill(source: Node = null)
    {
        this.OnKill(source)
        this.node.destroy()
    }
}